package com.zzyl.serve.controller.member;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.serve.domain.FamilyMember;
import com.zzyl.serve.domain.FamilyMemberElder;
import com.zzyl.serve.dto.FamilyElderDto;
import com.zzyl.serve.dto.MemberLoginDto;
import com.zzyl.serve.service.IDeviceService;
import com.zzyl.serve.service.IFamilyMemberService;
import com.zzyl.serve.vo.FamilyElderPageVo;
import com.zzyl.serve.vo.FamilyElderVo;
import com.zzyl.serve.vo.MemberLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 老人家属Controller
 *
 * @author liuyp
 * @date 2025-08-21
 */
@Api(tags = "老人家属管理")
@RestController
@RequestMapping("/member/user")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;
    @Autowired
    private IDeviceService deviceService;


    @PostMapping("/login")
    @ApiOperation("小程序登录")
    public R<MemberLoginVo> login(@RequestBody MemberLoginDto dto) {
        MemberLoginVo vo = familyMemberService.login(dto);
        return R.ok(vo);
    }

    /**
     * 查询老人家属列表
     */
    @ApiOperation("查询老人家属列表")
    @PreAuthorize("@ss.hasPermi('nursing:member:list')")
    @GetMapping("/list")
    public TableDataInfo<List<FamilyMember>> list(@ApiParam("老人家属查询条件") FamilyMember familyMember) {
        startPage();
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        return getDataTable(list);
    }

    /**
     * 导出老人家属列表
     */
    @ApiOperation("导出老人家属列表")
    @PreAuthorize("@ss.hasPermi('nursing:member:export')")
    @Log(title = "老人家属", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ApiParam(value = "老人家属查询条件") HttpServletResponse response, FamilyMember familyMember) {
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        ExcelUtil<FamilyMember> util = new ExcelUtil<FamilyMember>(FamilyMember.class);
        util.exportExcel(response, list, "老人家属数据");
    }

    /**
     * 获取老人家属详细信息
     */
    @ApiOperation("获取老人家属详细信息")
    @PreAuthorize("@ss.hasPermi('nursing:member:query')")
    @GetMapping(value = "/{id}")
    public R<FamilyMember> getInfo(@ApiParam("老人家属ID") @PathVariable("id") Long id) {
        return R.ok(familyMemberService.selectFamilyMemberById(id));
    }

    /**
     * 新增老人家属
     */
    @ApiOperation("新增老人家属")
    @PreAuthorize("@ss.hasPermi('nursing:member:add')")
    @Log(title = "老人家属", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("老人家属信息") @RequestBody FamilyMember familyMember) {
        return toAjax(familyMemberService.insertFamilyMember(familyMember));
    }

    /**
     * 修改老人家属
     */
    @ApiOperation("修改老人家属")
    @PreAuthorize("@ss.hasPermi('nursing:member:edit')")
    @Log(title = "老人家属", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("老人家属信息") @RequestBody FamilyMember familyMember) {
        return toAjax(familyMemberService.updateFamilyMember(familyMember));
    }

    /**
     * 删除老人家属
     */
    @ApiOperation("删除老人家属")
    @PreAuthorize("@ss.hasPermi('nursing:member:remove')")
    @Log(title = "老人家属", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("老人家属ID数组") @PathVariable Long[] ids) {
        return toAjax(familyMemberService.deleteFamilyMemberByIds(ids));
    }

    /**
     * 绑定家人-小程序
     */
    @ApiOperation("绑定家人")
    @PostMapping("/add")
    public AjaxResult bindFamily(@RequestBody FamilyElderDto dto){
        familyMemberService.bindFamily(dto);
        return AjaxResult.success();
    }

    /**
     * 查询家人列表-小程序
     */
    @ApiOperation("查询家人列表")
    @GetMapping("/my")
    public R<List<FamilyElderVo>> getFamilyList(){
        List<FamilyElderVo> res = familyMemberService.getFamilyList();
        return R.ok(res) ;
    }


    /**
     * 查看已绑定家人列表（分页查询）-小程序
     */
    @ApiOperation("查看已绑定家人列表")
    @GetMapping("/list-by-page")
    public R<List<FamilyElderPageVo>> listByPage(Integer pageNum, Integer pageSize) {
        List<FamilyElderPageVo> res = familyMemberService.listByPage(pageNum, pageSize);
        return R.ok(res);
    }

    /**
     * 家属解绑老人-小程序
     */
    @ApiOperation("家属解绑老人")
    @DeleteMapping("/deleteById")
    public AjaxResult deleteGetById(Long id){
        familyMemberService.deleteGetById(id);
        return success();
    }

}
